package com.bitsfromspace.ubertorrent.utils;


/**
 * Author: chris.oudeman
 * Since: Jan 6, 2011
 */
public class ByteUtils {

	private final static byte[] HEXABET =
			{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	/**
	 * Encodes a byte string to a friendly URL format, e.g.: %12%A2%3E
	 * 
	 */
	public static String urlEncodeByteString(byte[] data) {

		StringBuilder buffer = new StringBuilder(data.length * 3);

		for (byte b : data) {
			buffer.append("%");
			buffer.append((char) HEXABET[(b & 0xff) >> 4]);
			buffer.append((char) HEXABET[(b & 0xff) & 0xf]);
		}
		return buffer.toString();
	}

}


